//
//  RootViewController.m
//  CoreAnimationDemo
//
//  Created by AY on 17/5/31.
//  Copyright © 2017年 Alex. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"ID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];
    }
    cell.textLabel.text = self.dataSource[indexPath.row][@"title"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Class clazz = NSClassFromString(self.dataSource[indexPath.row][@"destController"]);
    UIViewController *vc = (UIViewController *)[clazz new];
    vc.title = self.dataSource[indexPath.row][@"title"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data.json" ofType:nil];
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:filePath] options:0 error:nil];
        NSArray *array = dictionary[@"data"];
        [self.dataSource addObjectsFromArray:array];
    }
    return _dataSource;
}
@end
